package com.example.xiaxiamusicplayer.utils;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.util.Log;

import com.example.xiaxiamusicplayer.bean.Mp3;

import java.io.IOException;
import java.util.List;

public class XiaXiaPlayer {
    private static final String TAG = "XiaXiaPlayer";
    private MediaPlayer mediaPlayer;
    private Mp3 currentMp3;
    private List<Mp3> allMp3;

    private static class Singleton {
        static final XiaXiaPlayer context = new XiaXiaPlayer();
    }

    public static XiaXiaPlayer getInstance() {
        return XiaXiaPlayer.Singleton.context;
    }

    private XiaXiaPlayer() {
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.start();
            }
        });
        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                mp.start();
            }
        });
        mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                return false;
            }
        });
    }

    public Mp3 getCurrentMp3() {
        return currentMp3;
    }

    public int getCurrentPosition() {
        return mediaPlayer.getCurrentPosition();
    }

    public boolean isPlaying() {
        return mediaPlayer.isPlaying();
    }

    public int getDuration() {
        return mediaPlayer.getDuration();
    }

    public void pause() {
        mediaPlayer.pause();
    }

    public void start() {
        mediaPlayer.start();
    }

    public void seekTo(int time) {
        mediaPlayer.seekTo(time);
    }

    public void setAllMp3(List<Mp3> allMp3) {
        this.allMp3 = allMp3;
    }

    public void startPlay(Mp3 mp3) {
        currentMp3 = mp3;
        Log.d(TAG, "当前播放:" + mp3.name);
        try {
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.stop();
            }
            mediaPlayer.reset();
            mediaPlayer.setDataSource(mp3.path);
            mediaPlayer.prepareAsync();
            Log.d(TAG, "STATE_PREPARING");
        } catch (IOException e) {
            e.printStackTrace();
            Log.e(TAG, "打开播放器发生错误", e);
        }
    }

    public void playNext() {
        int index = allMp3.indexOf(currentMp3);
        if (index == allMp3.size() - 1) {
            index = 0;
        } else {
            index++;
        }
        Mp3 mp3 = allMp3.get(index);
        startPlay(mp3);
    }

    public void playLast() {
        int index = allMp3.indexOf(currentMp3);
        if (index == 0) {
            index = allMp3.size() - 1;
        } else {
            index--;
        }
        Mp3 mp3 = allMp3.get(index);
        startPlay(mp3);
    }

    public void releasePlayer() {
        if (mediaPlayer != null) {
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }
}
